package com.wyzz.hr.action;

import java.util.Map;
import org.apache.commons.lang.StringUtils;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.util.DatabaseSysConfigManager;
import com.wyzz.hr.util.SysConfigManager;

public class ConfigExaminShift extends BaseAction {

	public ConfigExaminShift() {
	}

	public String showExaminShift() throws Exception {
		if (!hasAuth(961, new int[0])) {
			addErrorInfo("\u5BF9\u4E0D\u8D77\uFF0C\u60A8\u65E0\u6743\u8FDB\u5165\u8BE5\u9875\u9762\uFF01");
			return "noauth";
		}
		Map dbMap = dbManager.getProperties();
		sys_shift_late_minute = (String) dbMap.get("sys.shift.late.minute");
		sys_shift_late_minute = SystemConfigInit.getValue(
				sys_shift_late_minute, "");
		if (!sys_shift_late_minute.equals("")) {
			String str[] = splitString(sys_shift_late_minute, 6);
			firstLateBegin = str[0];
			firstLateEnd = str[1];
			secondLateBegin = str[2];
			secondLateEnd = str[3];
			thirdLateBegin = str[4];
			thirdLateEnd = str[5];
		}
		sys_shift_earlyleave_minute = (String) dbMap
				.get("sys.shift.earlyleave.minute");
		sys_shift_earlyleave_minute = SystemConfigInit.getValue(
				sys_shift_earlyleave_minute, "");
		if (!sys_shift_earlyleave_minute.equals("")) {
			String str[] = splitString(sys_shift_earlyleave_minute, 6);
			firstEarlyLeaveBegin = str[0];
			firstEarlyLeaveEnd = str[1];
			secondEarlyLeaveBegin = str[2];
			secondEarlyLeaveEnd = str[3];
			thirdEarlyLeaveBegin = str[4];
			thirdEarlyLeaveEnd = str[5];
		}
		sys_examin_absent_minute = (String) dbMap
				.get("sys.examin.absent.minute");
		sys_examin_absent_minute = SystemConfigInit.getValue(
				sys_examin_absent_minute, "");
		sys_shift_start_minute = (String) dbMap.get("sys.shift.start.minute");
		sys_shift_start_minute = SystemConfigInit.getValue(
				sys_shift_start_minute, "");
		sys_shift_end_minute = (String) dbMap.get("sys.shift.end.minute");
		sys_shift_end_minute = SystemConfigInit.getValue(sys_shift_end_minute,
				"");
		sys_shift_cardrepeat_minute = (String) dbMap
				.get("sys.shift.cardrepeat.minute");
		sys_shift_cardrepeat_minute = SystemConfigInit.getValue(
				sys_shift_cardrepeat_minute, "");
		sys_shift_card_calchour = (String) dbMap.get("sys.shift.card.calchour");
		sys_shift_card_calchour = SystemConfigInit.getValue(
				sys_shift_card_calchour, "");
		if (!sys_shift_card_calchour.equals("")) {
			String temp[] = splitString(sys_shift_card_calchour, 2);
			sys_shift_asHour = temp[0];
			sys_shift_asHalfHour = temp[1];
		}
		sys_shift_card_calcday = (String) dbMap.get("sys.shift.card.calcday");
		if (!StringUtils.isEmpty(sys_shift_card_calcday)) {
			String temp[] = splitString(sys_shift_card_calcday, 4);
			sys_shift_asDay = temp[0];
			sys_shift_asHalfDay = temp[1];
		}
		sys_examin_hourtoday_mode = StringUtils.defaultIfEmpty((String) dbMap
				.get("sys.examin.hourtoday.mode"), "0");
		sys_examin_leave_mode = StringUtils.defaultIfEmpty((String) dbMap
				.get("sys.examin.leave.mode"), "8");
		return "success";
	}

	public String updateExaminShift() throws Exception {
		if (!hasAuth(961, new int[0])) {
			addErrorInfo("\u5BF9\u4E0D\u8D77\uFF0C\u60A8\u65E0\u6743\u8FDB\u5165\u8BE5\u9875\u9762\uFF01");
			return "noauth";
		}
		Map dbMap = dbManager.getProperties();
		String temp_late_minute[] = { firstLateBegin, firstLateEnd,
				secondLateBegin, secondLateEnd, thirdLateBegin, thirdLateEnd };
		sys_shift_late_minute = combineString(temp_late_minute);
		if (!((String) dbMap.get("sys.shift.late.minute"))
				.equals(sys_shift_late_minute))
			dbManager.setProperty("sys.shift.late.minute",
					sys_shift_late_minute);
		String temp_earlyleave_minute[] = { firstEarlyLeaveBegin,
				firstEarlyLeaveEnd, secondEarlyLeaveBegin, secondEarlyLeaveEnd,
				thirdEarlyLeaveBegin, thirdEarlyLeaveEnd };
		sys_shift_earlyleave_minute = combineString(temp_earlyleave_minute);
		if (!((String) dbMap.get("sys.shift.earlyleave.minute"))
				.equals(sys_shift_earlyleave_minute))
			dbManager.setProperty("sys.shift.earlyleave.minute",
					sys_shift_earlyleave_minute);
		if (!((String) dbMap.get("sys.examin.absent.minute"))
				.equals(sys_examin_absent_minute))
			dbManager.setProperty("sys.examin.absent.minute",
					sys_examin_absent_minute);
		if (!((String) dbMap.get("sys.shift.start.minute"))
				.equals(sys_shift_start_minute))
			dbManager.setProperty("sys.shift.start.minute",
					sys_shift_start_minute);
		if (!((String) dbMap.get("sys.shift.end.minute"))
				.equals(sys_shift_end_minute))
			dbManager.setProperty("sys.shift.end.minute", sys_shift_end_minute);
		if (!((String) dbMap.get("sys.shift.cardrepeat.minute"))
				.equals(sys_shift_cardrepeat_minute))
			dbManager.setProperty("sys.shift.cardrepeat.minute",
					sys_shift_cardrepeat_minute);
		if (StringUtils.isEmpty(sys_shift_asHour)
				|| StringUtils.isEmpty(sys_shift_asHalfHour))
			sys_shift_card_calchour = "";
		else
			sys_shift_card_calchour = (new StringBuilder()).append(
					sys_shift_asHour).append(",").append(sys_shift_asHalfHour)
					.toString();
		if (!((String) dbMap.get("sys.shift.card.calchour"))
				.equals(sys_shift_card_calchour))
			dbManager.setProperty("sys.shift.card.calchour",
					sys_shift_card_calchour);
		if (!((String) dbMap.get("sys.examin.leave.mode"))
				.equals(sys_examin_leave_mode))
			dbManager.setProperty("sys.examin.leave.mode",
					sys_examin_leave_mode);
		if (!((String) dbMap.get("sys.examin.hourtoday.mode"))
				.equals(sys_examin_hourtoday_mode))
			dbManager.setProperty("sys.examin.hourtoday.mode",
					sys_examin_hourtoday_mode);
		if (StringUtils.isEmpty(sys_shift_asDay)
				|| StringUtils.isEmpty(sys_shift_asHalfDay))
			sys_shift_card_calcday = "";
		else
			sys_shift_card_calcday = (new StringBuilder()).append(
					sys_shift_asDay).append(",").append(sys_shift_asHalfDay)
					.toString();
		dbManager.setProperty("sys.shift.card.calcday", sys_shift_card_calcday);
		return "success";
	}

	public String[] splitString(String str, int length) {
		String s[] = new String[length];
		for (int i = 0; i < s.length; i++)
			s[i] = "";

		String temp1[] = str.split(";");
		for (int i = 0; i < temp1.length; i++) {
			String temp2[] = temp1[i].split(",");
			s[2 * i] = temp2[0];
			if (temp2.length > 1)
				s[2 * i + 1] = temp2[1];
		}

		return s;
	}

	public String combineString(String str[]) {
		String temp = "";
		String blank = "";
		String semicoln = "";
		for (int i = 0; i < str.length; i++) {
			blank = "";
			semicoln = "";
			if (i % 2 == 1) {
				if (!str[i].equals(""))
					blank = ",";
				if (i != str.length - 1)
					semicoln = ";";
			}
			temp = (new StringBuilder()).append(temp).append(blank).append(
					str[i]).append(semicoln).toString();
		}

		return temp;
	}

	public String getSys_shift_late_minute() {
		return sys_shift_late_minute;
	}

	public void setSys_shift_late_minute(String sys_shift_late_minute) {
		this.sys_shift_late_minute = sys_shift_late_minute;
	}

	public String getSys_shift_earlyleave_minute() {
		return sys_shift_earlyleave_minute;
	}

	public void setSys_shift_earlyleave_minute(
			String sys_shift_earlyleave_minute) {
		this.sys_shift_earlyleave_minute = sys_shift_earlyleave_minute;
	}

	public String getSys_shift_start_minute() {
		return sys_shift_start_minute;
	}

	public void setSys_shift_start_minute(String sys_shift_start_minute) {
		this.sys_shift_start_minute = sys_shift_start_minute;
	}

	public String getSys_shift_end_minute() {
		return sys_shift_end_minute;
	}

	public void setSys_shift_end_minute(String sys_shift_end_minute) {
		this.sys_shift_end_minute = sys_shift_end_minute;
	}

	public String getSys_shift_cardrepeat_minute() {
		return sys_shift_cardrepeat_minute;
	}

	public void setSys_shift_cardrepeat_minute(
			String sys_shift_cardrepeat_minute) {
		this.sys_shift_cardrepeat_minute = sys_shift_cardrepeat_minute;
	}

	public String getSys_shift_card_calchour() {
		return sys_shift_card_calchour;
	}

	public void setSys_shift_card_calchour(String sys_shift_card_calchour) {
		this.sys_shift_card_calchour = sys_shift_card_calchour;
	}

	public String getSys_shift_card_calcday() {
		return sys_shift_card_calcday;
	}

	public void setSys_shift_card_calcday(String sys_shift_card_calcday) {
		this.sys_shift_card_calcday = sys_shift_card_calcday;
	}

	public String getFirstLateBegin() {
		return firstLateBegin;
	}

	public void setFirstLateBegin(String firstLateBegin) {
		this.firstLateBegin = firstLateBegin;
	}

	public String getFirstLateEnd() {
		return firstLateEnd;
	}

	public void setFirstLateEnd(String firstLateEnd) {
		this.firstLateEnd = firstLateEnd;
	}

	public String getSecondLateBegin() {
		return secondLateBegin;
	}

	public void setSecondLateBegin(String secondLateBegin) {
		this.secondLateBegin = secondLateBegin;
	}

	public String getSecondLateEnd() {
		return secondLateEnd;
	}

	public void setSecondLateEnd(String secondLateEnd) {
		this.secondLateEnd = secondLateEnd;
	}

	public String getThirdLateBegin() {
		return thirdLateBegin;
	}

	public void setThirdLateBegin(String thirdLateBegin) {
		this.thirdLateBegin = thirdLateBegin;
	}

	public String getThirdLateEnd() {
		return thirdLateEnd;
	}

	public void setThirdLateEnd(String thirdLateEnd) {
		this.thirdLateEnd = thirdLateEnd;
	}

	public String getFirstEarlyLeaveBegin() {
		return firstEarlyLeaveBegin;
	}

	public void setFirstEarlyLeaveBegin(String firstEarlyLeaveBegin) {
		this.firstEarlyLeaveBegin = firstEarlyLeaveBegin;
	}

	public String getFirstEarlyLeaveEnd() {
		return firstEarlyLeaveEnd;
	}

	public void setFirstEarlyLeaveEnd(String firstEarlyLeaveEnd) {
		this.firstEarlyLeaveEnd = firstEarlyLeaveEnd;
	}

	public String getSecondEarlyLeaveBegin() {
		return secondEarlyLeaveBegin;
	}

	public void setSecondEarlyLeaveBegin(String secondEarlyLeaveBegin) {
		this.secondEarlyLeaveBegin = secondEarlyLeaveBegin;
	}

	public String getSecondEarlyLeaveEnd() {
		return secondEarlyLeaveEnd;
	}

	public void setSecondEarlyLeaveEnd(String secondEarlyLeaveEnd) {
		this.secondEarlyLeaveEnd = secondEarlyLeaveEnd;
	}

	public String getThirdEarlyLeaveBegin() {
		return thirdEarlyLeaveBegin;
	}

	public void setThirdEarlyLeaveBegin(String thirdEarlyLeaveBegin) {
		this.thirdEarlyLeaveBegin = thirdEarlyLeaveBegin;
	}

	public String getThirdEarlyLeaveEnd() {
		return thirdEarlyLeaveEnd;
	}

	public void setThirdEarlyLeaveEnd(String thirdEarlyLeaveEnd) {
		this.thirdEarlyLeaveEnd = thirdEarlyLeaveEnd;
	}

	public String getSys_shift_asHour() {
		return sys_shift_asHour;
	}

	public void setSys_shift_asHour(String sys_shift_asHour) {
		this.sys_shift_asHour = sys_shift_asHour;
	}

	public String getSys_shift_asHalfHour() {
		return sys_shift_asHalfHour;
	}

	public void setSys_shift_asHalfHour(String sys_shift_asHalfHour) {
		this.sys_shift_asHalfHour = sys_shift_asHalfHour;
	}

	public String getsys_shift_asDay() {
		return sys_shift_asDay;
	}

	public void setsys_shift_asDay(String sys_shift_asDay) {
		this.sys_shift_asDay = sys_shift_asDay;
	}

	public String getsys_shift_asHalfDay() {
		return sys_shift_asHalfDay;
	}

	public void setsys_shift_asHalfDay(String sys_shift_asHalfDay) {
		this.sys_shift_asHalfDay = sys_shift_asHalfDay;
	}

	public String getSys_examin_absent_minute() {
		return sys_examin_absent_minute;
	}

	public void setSys_examin_absent_minute(String sys_examin_absent_minute) {
		this.sys_examin_absent_minute = sys_examin_absent_minute;
	}

	public String getSys_examin_leave_mode() {
		return sys_examin_leave_mode;
	}

	public void setSys_examin_leave_mode(String sys_examin_leave_mode) {
		this.sys_examin_leave_mode = sys_examin_leave_mode;
	}

	public String getSys_examin_hourtoday_mode() {
		return sys_examin_hourtoday_mode;
	}

	public void setSys_examin_hourtoday_mode(String sys_examin_hourtoday_mode) {
		this.sys_examin_hourtoday_mode = sys_examin_hourtoday_mode;
	}

	private static final long serialVersionUID = 1L;
	private String sys_shift_late_minute;
	private String firstLateBegin;
	private String firstLateEnd;
	private String secondLateBegin;
	private String secondLateEnd;
	private String thirdLateBegin;
	private String thirdLateEnd;
	private String sys_shift_earlyleave_minute;
	private String firstEarlyLeaveBegin;
	private String firstEarlyLeaveEnd;
	private String secondEarlyLeaveBegin;
	private String secondEarlyLeaveEnd;
	private String thirdEarlyLeaveBegin;
	private String thirdEarlyLeaveEnd;
	private String sys_examin_absent_minute;
	private String sys_shift_start_minute;
	private String sys_shift_end_minute;
	private String sys_shift_cardrepeat_minute;
	private String sys_shift_card_calchour;
	private String sys_shift_asHour;
	private String sys_shift_asHalfHour;
	private String sys_shift_card_calcday;
	private String sys_shift_asDay;
	private String sys_shift_asHalfDay;
	private String sys_examin_leave_mode;
	private String sys_examin_hourtoday_mode;
	private static SysConfigManager dbManager = DatabaseSysConfigManager
			.getInstance();

}
